//
//  ContactViewController.m
//  UI10_HomeWork
//
//  Created by Bodi on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ContactViewController.h"
#import "NameViewController.h"

@interface ContactViewController ()<UITableViewDataSource, UITableViewDelegate, ContactViewControllerDelegater>
@property (nonatomic, retain) NSArray *keyArray;
@property (nonatomic,retain) NSArray<UIImage *> *imageArray;
@end

@implementation ContactViewController
- (void)dealloc{
    [_dictionary release];
    [_keyArray release];
    [super dealloc];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handleData];
    [self createtabView];
    
}
- (void)config{
    self.dictionary = [[NSDictionary alloc]init];
    self.keyArray = [[NSArray alloc]init];
    self.imageArray = [[NSArray alloc]init];
    self.cell = [[UITableViewCell alloc]init];
    self.navigationItem.title = @"联系人";
    self.view.backgroundColor = [UIColor grayColor];
    self.automaticallyAdjustsScrollViewInsets = NO;



}

//数据处理
- (void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@"plist"];
    self.dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    self.keyArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    
    
    
//    NSLog(@"%@", _keyArray);
    NSLog(@"%@", _imageArray);


}

//创建视图
- (void)createtabView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 70;
    self.tableView.separatorColor = [UIColor blackColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dictionary.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.keyArray objectAtIndex:section];
    NSArray *array = [self.dictionary objectForKey:key];
    return array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.arry = [self.dictionary objectForKey:[self.keyArray objectAtIndex:indexPath.section]];
   self.dic = [self.arry objectAtIndex:indexPath.row];
    self.cell.textLabel.text = [self.dic objectForKey:@"name"];
    
    
//    self.cell.textLabel.text = self.str;
    
    
    
    NSDictionary *dic1 = [self.arry objectAtIndex:indexPath.row];
    NSString *str = [dic1 objectForKey:@"header"];
    self.cell.imageView.layer.cornerRadius = 35;
    self.cell.imageView.layer.masksToBounds = YES;
    self.cell.imageView.frame = CGRectMake(0, 0, 5, 5);
    self.cell.imageView.image =[UIImage imageNamed:str];
//    cell.imageView.image = [dic objectForKey:@"header"];
   self. cell.accessoryType = UITableViewCellAccessoryDetailButton;
    return self.cell;


}
-(UIImage *) reSizeImage:(UIImage *)image andSize:(CGSize)reSize {
    
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
    
    [image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];
    
    UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *headerString = [self.keyArray objectAtIndex:section];
    return headerString;

}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keyArray;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NameViewController *nam = [[NameViewController alloc]init];
    [self.navigationController pushViewController:nam animated:YES];
    NSArray *arry = [self.dictionary objectForKey:[self.keyArray objectAtIndex:indexPath.section]];
    
    NSDictionary *dic = [arry objectAtIndex:indexPath.row];
    NSString *strName = [dic objectForKey:@"name"];
    
    NSDictionary *dic1 = [arry objectAtIndex:indexPath.row];
    NSString *strImage = [dic1 objectForKey:@"header"];
    
    NSDictionary *dic2 = [arry objectAtIndex:indexPath.row];
    NSString *strNumber = [dic2 objectForKey:@"number"];
    NSDictionary *dic3 = [arry objectAtIndex:indexPath.row];
    NSString *strQq = [dic3 objectForKey:@"QQ"];
    nam.strName = strName;
    nam.strNumber = strNumber;
    nam.strQq = strQq;
    nam.strImage = strImage;
   nam.delegate = self;

    
    

}

- (void)change:(NSString *)name{
    
    
    self.cell.textLabel.text = name;
    self.str= name;
   
    NSLog(@"%@", _str);
    [self.tableView reloadData];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
